package com.bosssoft.ct.springcloud.controller;

import com.bosssoft.ct.springcloud.pojo.dto.UserDTO;
import com.bosssoft.ct.springcloud.pojo.query.UserQuery;
import com.bosssoft.ct.springcloud.service.UserFeignService;
import com.bosssoft.ct.springcloud.utils.http.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 用户服务消费逻辑控制器
 *
 * @author ChenTong
 * @version 1.0
 * @date 2020/6/22 15:46
 * @copyright
 * @modified
 * @see UserFeignService
 * @since
 **/
@RestController
@Slf4j
public class UserController {
    public static final String USER_URL = "http://CLOUD-ADMIN-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private UserFeignService feignService;

    /**
     * 使用简单的resttemplate(roibbon 进行服务调用)
     * @author ChenTong
     * @param query
     * @return com.bosssoft.ct.springcloud.utils.http.HttpResult
     * @date 2020/6/22 21:03
     */
    @GetMapping(value = "/consumer/user")
    public HttpResult getUsers(UserQuery query){
        return this.restTemplate.getForObject(USER_URL+"/user", HttpResult.class, query);
    }

    /**
     * 使用openfeign基于接口进行调用服务
     * @author ChenTong
     * @param query
     * @return com.bosssoft.ct.springcloud.utils.http.HttpResult
     * @date 2020/6/22 21:03
     */
    @GetMapping(value = "/feign/user")
    public HttpResult getUser(UserQuery query){
        return this.feignService.getUsers(query);
    }

    /**
     * 使用openfeign调用更新服务
     * @author ChenTong
     * @param userDTO 
     * @return com.bosssoft.ct.springcloud.utils.http.HttpResult
     * @date 2020/6/23 8:10
     */
    @PutMapping(value = "/feign/user")
    public HttpResult updateUser(UserDTO userDTO){
        return this.feignService.update(userDTO);
    }

    /**
     * 使用openfeign调用删除操作
     * @author ChenTong
     * @param id 
     * @return com.bosssoft.ct.springcloud.utils.http.HttpResult
     * @date 2020/6/23 8:11
     */
    @DeleteMapping(value = "/feign/user/{id}")
    public HttpResult deleteUser(@PathVariable("id") Long id){
        return this.feignService.delete(id);
    }

    /**
     * 使用openfeign调用添加操作
     * @author ChenTong
     * @param userDTO
     * @return com.bosssoft.ct.springcloud.utils.http.HttpResult
     * @date 2020/6/23 8:11
     */
    @PostMapping(value = "/feign/user")
    public HttpResult insertUser(@RequestBody UserDTO userDTO){
        return this.feignService.insert(userDTO);
    }
}
